#include "gui/Roulette.h"

#include "utilities/Rectangle.h"

namespace gui
{
	Roulette::Roulette() : imageSetLeft( NULL ), imageSetRight( NULL ), imageDisplay( NULL )
	{
	}

	Roulette::Roulette( const gdn::Vector2f& thePos, std::vector< std::string > theOptions, int theDefaultOption,
					    gdn::Image& theImageLeftSet, gdn::Image& theImageRightSet, gdn::Image& theImageDisplay, gdn::Font& theFont, int theCharSize, gdn::Color theTextColor,
					    const gdn::Rectanglei& theRectLeftIdle, const gdn::Rectanglei& theRectLeftHover, const gdn::Rectanglei& theRectLeftPress,
					    const gdn::Rectanglei& theRectRightIdle, const gdn::Rectanglei& theRectRightHover, const gdn::Rectanglei& theRectRightPress )
	   : pos( thePos ), mousePos( 0, 0 ), options( theOptions ), defaultOption( theDefaultOption ), current( defaultOption ),
	     imageSetLeft( &theImageLeftSet ), imageSetRight( &theImageRightSet ), imageDisplay( &theImageDisplay ), font( &theFont ), charSize( theCharSize ), textColor( theTextColor ),
	     rectLeftIdle( theRectLeftIdle ), rectLeftHover( theRectLeftHover ), rectLeftPress( theRectLeftPress ),
	     rectRightIdle( theRectRightIdle ), rectRightHover( theRectRightHover ), rectRightPress( theRectRightPress ),
	     activeRectLeft( &rectLeftIdle ), activeRectRight( &rectRightIdle )

	{
		buttonLeft = gui::Button( pos,
			                      "", "", "",
					              gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ),
					              *imageSetLeft, *font, 0,
					              rectLeftIdle, rectLeftHover, rectLeftPress );

		buttonRight = gui::Button( gdn::Vector2f( pos.x + rectLeftIdle.width + imageDisplay->GetSize().x, pos.y ),
			                       "", "", "",
					               gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ),
					               *imageSetRight, *font, 0,
					               rectRightIdle, rectRightHover, rectRightPress );

		displaySprite.SetImage( *imageDisplay );
		displaySprite.SetPosition( gdn::Vector2f( pos.x + rectLeftIdle.width, pos.y ) );

		displayText.SetFont( *font );
		displayText.SetCharacterSize( charSize );
		displayText.SetColor( textColor );

		displayText.SetString( options[ current ] );

		RecenterText();
	}

	int Roulette::Process( const gdn::Event& event )
	{
		int ret = GUI_ROULETTE_IDLE;

		if ( buttonLeft.Process( event ) == GUI_BUTTON_RELEASE )
		{
			ret = GUI_ROULETTE_LEFT;
			if ( current == 0 )
			{
				current = options.size() - 1;
			}
			else
			{
				--current;
			}
		}
		if ( buttonRight.Process( event ) == GUI_BUTTON_RELEASE )
		{
			ret = GUI_ROULETTE_RIGHT;
			++current;
		}

		if ( ret != GUI_ROULETTE_IDLE )
		{
			if ( current < 0 )
			{
				current = options.size() - 1;
			}
			else if ( current >= options.size() )
			{
				current = 0;
			}

			displayText.SetString( GetCurrentItem() );
			RecenterText();
		}

		return ret;
	}

	void Roulette::Draw( gdn::Window& window )
	{
		buttonLeft.Draw( window );

		window.Draw( displaySprite );
		window.Draw( displayText );

		buttonRight.Draw( window );
	}

	void Roulette::Draw( gdn::RenderImage& renderImage )
	{
		buttonLeft.Draw( renderImage );

		renderImage.Draw( displaySprite );
		renderImage.Draw( displayText );

		buttonRight.Draw( renderImage );
	}

	std::string Roulette::GetCurrentItem()
	{
		return options[ current ];
	}


	void Roulette::RecenterText()
	{
		gdn::Rectanglef textRect = displayText.GetRectangle();
		gdn::Vector2< unsigned int > displaySize = imageDisplay->GetSize();

		gdn::Vector2f centered( ( displaySize.x - textRect.width ) / 2, ( displaySize.y - textRect.height ) / 2 );

		displayText.SetPosition( gdn::Vector2f( ( int )( pos.x + rectLeftIdle.width + centered.x ), ( int )( pos.y + centered.y ) ) );
	}
}

